package linked;
import java.util.*;
public class ListNode {
     
    public int val;
    public ListNode next;
    public ListNode() {}
    public ListNode(int val) { this.val = val; }
    public ListNode(int val, ListNode next) { this.val = val; this.next = next; }

    public static ListNode newListNodeFromArray(int[] vals) {
        ListNode head = new ListNode(0), cur = head;
        for (int val: vals) {
            cur.next = new ListNode(val);
            cur = cur.next;
        }
        return head.next;
    }

    public static List<ListNode> newListNodeFromArray(int[] vals, boolean needList) {
        ListNode head = new ListNode(0), cur = head;
        List<ListNode> res = new ArrayList<>();
        for (int val: vals) {
            cur.next = new ListNode(val);
            cur = cur.next;
            res.add(cur);
        }
        return res;
    }

    public String toString() {
        ListNode cur = this;
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        while (cur != null) {
            sb.append(cur.val + ", ");
            cur = cur.next;
        }
        sb.deleteCharAt(sb.length() - 1);
        sb.deleteCharAt(sb.length() - 1);
        sb.append("]");
        return sb.toString();
    }
}
